package oot.activity;

import java.util.ArrayList;

import oot.data.Config;
import oot.data.Connection;
import oot.data.JSonPaser;
import oot.data.Tweet;
import oot.data.User;
import oot.tab.activity.R;
import oot.view.HeadTab;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ott.adapter.TweetAdapter;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class ViewTweetFavoriteActivity extends Activity {
	private ArrayList<Tweet> listTweets;
	private TweetAdapter adapter;
	private Context mContext;
	private PullToRefreshListView mPullRefreshListView;
	private JSONObject js;
	private int mPage = 1;
	private Bundle mBundle;
	private String api;
	private User mUser;
	private RequestParams params;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		api = "/Favorites/get_favorite_tweet";
		listTweets = new ArrayList<Tweet>();
		mContext = getApplicationContext();
		adapter = new TweetAdapter(mContext, listTweets);
		setContentView(R.layout.user_favorite_tweet_layout);
		HeadTab headTab = new HeadTab(this);
		headTab.createHeadTabAction("Favorite");
		mUser = (User) getIntent().getSerializableExtra(Config.USER);
		mPullRefreshListView = (PullToRefreshListView)findViewById(R.id.lv_user_favo_tweet);
		mPullRefreshListView.getRefreshableView().setAdapter(adapter);
		getData(mUser.getUserID(),"1","10");
		if (mBundle != null) {
			int firstItem = mBundle.getInt("firstelement");
			// Log.i("test","first item "+ firstItem);
			mPullRefreshListView.getRefreshableView().setAdapter(adapter);
			mPullRefreshListView.getRefreshableView().setSelectionFromTop(
					firstItem, 0);
		}
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						// mPullRefreshListView.setLastUpdatedLabel("Loading...");
						listTweets.clear();
						mPage = 1;
						getData(mUser.getUserID(),
								""+mPage, "10");
					}
				});

		// Add an end-of-list listener
		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

					@Override
					public void onLastItemVisible() {
						Log.i("test", "last item viewed");
						
						getData(FlashScreenActivity.user.getUserID(),
								""+mPage, "6");
					}
				});
	}

	protected void getData(String userId, String page, String count) {
		params = new RequestParams();
		params.put("user_id", userId);
		params.put("page", page);
		params.put("count", count);

		 Log.i("test", "params :" + params.toString());
		Connection.post(api, params, new AsyncHttpResponseHandler() {
			public void onSuccess(String arg0) {
				 Log.i("test", "return --------" + arg0);
				super.onSuccess(arg0);
				try {
					js = new JSONObject(arg0);
					if (js.getInt("status_code") != 0) {
						// show error message if error occur
						Toast toast = Toast.makeText(mContext,
								js.getString("message"), Toast.LENGTH_SHORT);
						toast.show();
					} else {
						// get timeline
						JSONArray jsTweets = (JSONArray) js.get("entity");

						for (int i = 0; i < jsTweets.length(); i++) {
							// add to list tweet
							listTweets.add(JSonPaser
									.paserTweet((JSONObject) jsTweets.get(i)));
						}

						if (!listTweets.isEmpty())
							mPage++;
						Log.i("test", "min tweetid" + mPage);
						adapter.notifyDataSetChanged();
						// Call onRefreshComplete when the list has been
						// refreshed.
						mPullRefreshListView.onRefreshComplete();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Throwable arg0, String arg1) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1);
				Log.i("test", "jumb to onfailure");
				mPullRefreshListView.onRefreshComplete();
			}
		});
	}
}
